package com.eversec.view.render;

import java.awt.Color;
import java.awt.Component;
import java.io.File;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JLabel;
import javax.swing.JList;

import com.eversec.util.resource.ResourceUtils;

/**
 * 列表渲染类
 * @author lilz-2686
 *
 */
public class ListCellRenderer extends DefaultListCellRenderer {
	
	private static final long serialVersionUID = -3249940236543127169L;

	public Component getListCellRendererComponent(JList list, Object value,
			int index, boolean isSelected, boolean cellHasFocus) {
		JLabel label = (JLabel)super.getListCellRendererComponent(list, 
				value, index, isSelected, cellHasFocus);
		File listFile = (File) value;
		
		setIcon(label, listFile);
		label.setText(listFile.getName());
		
		if (isSelected) {
			setBackground(new Color(47, 106, 197));
			setForeground(new Color(255, 255, 255));
		}
		return this;
	}

	/**
	 * 设置图标
	 * @param label 
	 * @param listFile
	 */
	private void setIcon(JLabel label, File listFile) {
		if (listFile.isDirectory()) {
			label.setIcon(ResourceUtils.FOLDER_IMAGE);
		} else if (listFile.isFile()) {
			String listName = listFile.getName();
			if (listName.toLowerCase().endsWith(".ok")) {
				label.setIcon(ResourceUtils.XML_OK_IMAGE);
			} else if (listName.toLowerCase().endsWith(".err")) {
				label.setIcon(ResourceUtils.XML_ERR_IMAGE);
			} else {
				label.setIcon(ResourceUtils.OTHER_IMAGE);
			}
		}
	}
}
